package com.adam.test.example.impl;

import com.adam.test.entity.User1;
import com.adam.test.example.PropagationSelectExample;
import com.adam.test.service.PropagationAddUser1Service;
import com.adam.test.service.PropagationSelectUser1Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author jiangwh
 */
@Service
public class PropagationSelectExampleImpl implements PropagationSelectExample {
    
    private final PropagationAddUser1Service addUser1Service;
    
    
    private final PropagationSelectUser1Service selectUser1Service;
    
    
    @Autowired
    public PropagationSelectExampleImpl(PropagationAddUser1Service addUser1Service, PropagationSelectUser1Service selectUser1Service) {
        this.addUser1Service = addUser1Service;
        this.selectUser1Service = selectUser1Service;
    }
    
    @Override
    public void truncate() {
        addUser1Service.truncate();
    }

    @Override
    public boolean[] no_addRequired_getRequired_get() {
        boolean flag1 = false;
        boolean flag2 = false;
        addUser1Service.addRequired(createUser1());
        
        User1 res1 = selectUser1Service.getRequired(1);
        if (res1 != null) {
            flag1 = true;
            System.out.println("getRequired可见");
        }
        User1 res2 = selectUser1Service.get(1);
        if (res2 != null) {
            flag2 = true;
            System.out.println("get可见");
        }

        return new boolean[] { flag1, flag2 };
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public boolean[] required_addRequired_getRequired_get() {
        return no_addRequired_getRequired_get();
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public boolean[] required_addRequired_getNew_get() {
        boolean flag1 = false;
        boolean flag2 = false;
        addUser1Service.addRequired(createUser1());

        User1 res1 = selectUser1Service.getRequiresNew(1);
        if (res1 != null) {
            flag1 = true;
            System.out.println("getRequiresNew可见");
        }
        User1 res2 = selectUser1Service.get(1);
        if (res2 != null) {
            flag2 = true;
            System.out.println("get可见");
        }

        return new boolean[] { flag1, flag2 };
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public boolean[] required_addRequired_getNested_get() {
        boolean flag1 = false;
        boolean flag2 = false;
        addUser1Service.addRequired(createUser1());

        User1 res1 = selectUser1Service.getNested(1);
        if (res1 != null) {
            flag1 = true;
            System.out.println("getNested可见");
        }
        User1 res2 = selectUser1Service.get(1);
        if (res2 != null) {
            flag2 = true;
            System.out.println("get可见");
        }

        return new boolean[] { flag1, flag2 };
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public boolean[] required_addRequired_getSupports_get() {
        boolean flag1 = false;
        boolean flag2 = false;
        addUser1Service.addRequired(createUser1());

        User1 res1 = selectUser1Service.getSupports(1);
        if (res1 != null) {
            flag1 = true;
            System.out.println("getSupports可见");
        }
        User1 res2 = selectUser1Service.get(1);
        if (res2 != null) {
            flag2 = true;
            System.out.println("get可见");
        }

        return new boolean[] { flag1, flag2 };
    }

    private User1 createUser1() {
        User1 user = new User1();
        user.setName("张三");
        return user;
    }
    
}
